// gulp 的入口文件

const { series, parallel } = require('gulp');
const fs = require('fs')
exports.foo = done => {
    console.log('gulp foo task');
    done();  // 标识任务完成
};

exports.default = done => {
    console.log('gulp foo task');
    done();  // 标识任务完成
};


// 4.0之前需要使用 gulp.task 定义任务

const gulp = require('gulp');
gulp.task('bar', done => {
    console.log('bar task');
    done();
});


const task1 = done => {
    console.log('task1 = start')
    setTimeout(() => {
        console.log('task1 = end');
        done();
    }, 1000);
}
const task2 = done => {
    console.log('task2 = start')
    setTimeout(() => {
        console.log('task2 = end');
        done();
    }, 1000);
}
const task3 = done => {
    console.log('task3 = start')
    setTimeout(() => {
        console.log('task3 = end');
        done();
    }, 1000);
}

exports.chuan = series(task1, task2, task3);
exports.bing = parallel(task1, task2, task3);

exports.callback = done => {
    console.log('callback task');
    done();
};

exports.callback_error = done => {
    console.log('callback taskerr');
    done(new Error('task failed'));
};

exports.callback_error = done => {
    console.log('callback task');
    return Promise.resolve();
};

exports.callback_error = done => {
    console.log('callback task');
    return Promise.reject(new Error('task err'));
};

const timeout = time => {
    return new Promise(resolve => {
        setTimeout(resolve, time);
    })
}

exports.async = async () => {
    await timeout(1000);
}

exports.stream = done => {
    const readStream = fs.createReadStream('package.json');
    const writeStream = fs.createWriteStream('temp.txt');
    readStream.pipe(writeStream);
    readStream.on('end', () => {
        done();
    });
}